import { defineStore } from 'pinia';
import { createStorage, storage } from '@/utils/Storage';
import { store } from '@/store';
import { ACCESS_TOKEN, CURRENT_USER, IS_LOCKSCREEN } from '@/store/mutation-types';
import { ResultEnum } from '@/enums/httpEnum';
import loginApi from '@/api/loginApi';
import menuApi from '@/api/menuApi';

const Storage = createStorage({ storage: localStorage });

export interface IUserState {
  token: string;
  username: string;
  welcome: string;
  avatar: string;
  permissions: any[];
  info: any;
}

export const useUserStore = defineStore({
  id: 'app-user',
  state: (): IUserState => ({
    token: Storage.get(ACCESS_TOKEN, ''),
    username: '',
    welcome: '',
    avatar: '',
    permissions: [],
    info: Storage.get(CURRENT_USER, {}),
  }),
  getters: {
    getToken(): string {
      return this.token;
    },
    getAvatar(): string {
      return this.avatar;
    },
    getNickname(): string {
      return this.username;
    },
    getPermissions(): [any][] {
      return this.permissions;
    },
    getUserInfo(): object {
      return this.info;
    },
  },
  actions: {
    setToken(token: string) {
      this.token = token;
    },
    setAvatar(avatar: string) {
      this.avatar = avatar;
    },
    setPermissions(permissions) {
      this.permissions = permissions;
    },
    setUserInfo(info) {
      this.info = info;
    },
    // // 登录
    // async login(userInfo) {
    //   try {
    //     const response = await login(userInfo);
    //     const { result, code } = response;
    //     if (code === ResultEnum.SUCCESS) {
    //       const ex = 7 * 24 * 60 * 60 * 1000;
    //       storage.set(ACCESS_TOKEN, result.token, ex);
    //       storage.set(CURRENT_USER, result, ex);
    //       storage.set(IS_LOCKSCREEN, false);
    //       this.setToken(result.token);
    //       this.setUserInfo(result);
    //     }
    //     return Promise.resolve(response);
    //   } catch (e) {
    //     return Promise.reject(e);
    //   }
    // },

    // 获取用户信息
    async GetInfo() {
      const userMsg = await loginApi.getCurrentUser();
      const permissionsMsg = await menuApi.selectCurrentUserPermission(null);
      const user = userMsg.data;
      this.setUserInfo(user);
      if (permissionsMsg && permissionsMsg.data) {
        this.setPermissions(permissionsMsg.data.map((it) => it.resourceCode));
      }
      console.log('权限识别码', this);
      return user;
    },

    // 登出
    async logout() {
      this.setPermissions([]);
      this.setUserInfo('');
      storage.remove(ACCESS_TOKEN);
      storage.remove(CURRENT_USER);
      return Promise.resolve('');
    },
  },
});

// Need to be used outside the setup
export function useUserStoreWidthOut() {
  return useUserStore(store);
}
